#include "SphereCollider.h"
#include "Scene/SceneNode.h"

#include "Asset/EngineDefaultPackage.h"
#include "Mesh/StaticMesh.h"
#include "Render/Scene/RenderScene.h"
#include "Material/Material.h"

void RSphereCollider::PaintCollider(CRenderScene *InRenderData)
{
    auto Mesh=REngineDefaultPackage::Get()->GetSphereMesh();
    auto Material=REngineDefaultPackage::Get()->GetDefaultMaterial();

    SRenderMeshInstanceData& MeshInstanceData=InRenderData->MeshInstances.Emplace();
    MeshInstanceData.Rotation=GetNode()->GetWorldRotation();
    MeshInstanceData.Position=GetNode()->GetWorldPosition()+MeshInstanceData.Rotation*Center*GetNode()->GetScale();
    MeshInstanceData.Scale=GetNode()->GetScale() * Radius;

    MeshInstanceData.BoundingSphereRadius= MeshInstanceData.Scale.GetMax();
    MeshInstanceData.CastShadow=false;

    MeshInstanceData.MeshSections=Mesh->GetRenderMeshes();
    MeshInstanceData.Materials.Add(Material->GetRenderMaterial());

}

void RSphereCollider::SetRadius(float InRadius)
{
    Radius = InRadius;
}

float RSphereCollider::GetRadius() const
{
    return Radius;
}

const SVec3 &RSphereCollider::GetCenter() const
{
    return Center;
}

void RSphereCollider::SetCenter(const SVec3 &InCenter)
{
    Center = InCenter;
}

SVec3 RSphereCollider::GetCenterWorldPosition()
{
    return GetNode()->GetWorldTransform().TransformPosition(Center);
}